<template>
  <!-- 对话框(添加 / 修改) -->
  <el-dialog
    title="添加功能"
    :visible.sync="show"
    width="500px"
    append-to-body
    :before-close="cancel"
    :close-on-click-modal="false"
  >
    <el-row>
      <el-col
        :span="12"
        style="text-align: center; padding: 10px"
        v-for="(tag, index) in tags"
        :key="index"
      >
        <el-tag
          v-if="!tag.inputVisible"
          closable
          :disable-transitions="true"
          @close="handleClose(tag)"
        >
          {{ tag.name }}
        </el-tag>
        <el-input
          class="input-new-tag"
          v-if="tag.inputVisible"
          v-model="tag.name"
          ref="saveTagInput"
          size="small"
          @keyup.enter.native="handleInputConfirm(tag)"
          @blur="handleInputConfirm(tag)"
        >
        </el-input>
      </el-col>
      <el-col :span="24">
        <div style="float: right; margin: 20px">
          <el-button icon="el-icon-plus" @click="showInput">添加</el-button>
        </div>
      </el-col>
    </el-row>

    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="cancel">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { createClassroomFunction, deleteClassroomFunction } from "@/api/collegebase/classroomFunction"
import { randomUUID } from "crypto";
export default {
  name: "",
  components: {},
  data() {
    return {
      // tags: [
      //   { name: "标签一", type: "" },
      //   { name: "标签二", type: "success" },
      //   { name: "标签三", type: "info" },
      //   { name: "标签四", type: "warning" },
      //   { name: "标签五", type: "danger",inputVisible:false},
      // ],
      inputVisible: false,
      submitting: false,
    };
  },
  created() {

  },
  props: {
    // 是否显示弹出层
    show: {
      type: Boolean,
      default: false,
    },
    // 显示弹出层标题
    title: {
      type: String,
      default: "",
    },
    tags: {
      type: Array
    }
  },
  methods: {
    async handleInputConfirm(tag) {
      if (this.submitting) return;
      this.submitting = true;
      if (tag && tag.name) {
        tag.inputVisible = false;
        const res = await createClassroomFunction({ functionName: tag.name })
        if (res.data) {
          this.$parent.getTags();
        }
        this.submitting = false;

      } else {
        this.$message.error("请输入功能名称");
        this.submitting = false;
      }

    },
    handleClose(tag) {
      this.tags.forEach(async (e, i) => {
        if (e.name == tag.name) {
          const res = await deleteClassroomFunction(tag.id)
          if (res.data) {
            this.$parent.getTags();
          }
          this.tags.splice(i, 1);
        }
      })
      // this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
    },
    showInput() {
      this.inputVisible = true;
      this.tags.push({ name: "", type: "danger", inputVisible: true });
      this.submitting = false;
      this.$nextTick(() => {
        // console.log(this.$refs.saveTagInput)
        this.$refs.saveTagInput.at(-1).focus();
      });
    },
    cancel() {
      this.$emit("update:show", false);
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateClassroomInfo(this.form).then((response) => {
            this.$modal.msgSuccess("修改成功");
            this.cancel();
            this.$emit("success");
          });
          return;
        }
        // 添加的提交
        createClassroomInfo(this.form).then((response) => {
          this.$modal.msgSuccess("新增成功");
          this.cancel();
          this.$emit("success");
        });
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>
